package com.example.smartcommunity;

import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.smartcommunity.com.example.adapter.GuidePageAdapter;
import com.example.smartcommunity.com.example.fragment.StepOneFragment;
import com.example.smartcommunity.com.example.fragment.StepThreeFragment;
import com.example.smartcommunity.com.example.fragment.StepTwoFragment;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {



    private ViewPager mViewPager;

    private GuidePageAdapter mPageAdapter;

    private List<Fragment> mFragments;

    private LinearLayout mDotLayout;

    private ImageView[] dots;

    private int currentIndex;



    private void initView() {



        mViewPager = (ViewPager) findViewById(R.id.guide_view_pager);

        mDotLayout = (LinearLayout) findViewById(R.id.dots_layout);

    }



    private void initData() {



        mFragments = new ArrayList<Fragment>();

        mFragments.add(StepOneFragment.newInstance());

        mFragments.add(StepTwoFragment.newInstance());

        mFragments.add(StepThreeFragment.newInstance());

        mPageAdapter = new GuidePageAdapter(getSupportFragmentManager(), mFragments);

        mViewPager.setAdapter(mPageAdapter);

    }



    private void initListeners() {



        mViewPager.addOnPageChangeListener(this);

    }



    private void initDots() {



        dots = new ImageView[mFragments.size()];

        for (int i = 0; i < mFragments.size(); i++) {

            dots[i] = (ImageView) mDotLayout.getChildAt(i);

            if (i != 0) {

                dots[i].setImageResource(R.drawable.doc_01);

            }

        }

        currentIndex = 0;

        dots[currentIndex].setImageResource(R.drawable.doc_02);

    }



    private void setCurrentDot(int position) {



        if (position < 0 || position > mFragments.size() - 1

                || currentIndex == position) {

            return;

        }

        dots[position].setImageDrawable(null);

        dots[position].setImageResource(R.drawable.doc_02);

        dots[currentIndex].setImageDrawable(null);

        dots[currentIndex].setImageResource(R.drawable.doc_01);

        currentIndex = position;

    }



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_guide);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            // 透明状态栏

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            // 透明导航栏

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }


        initView();

        initData();

        initDots();

        initListeners();

        finish();

    }



    // 当前页面被滑动时调用

    @Override

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {



    }



    // 当新的页面被选中时调用

    @Override

    public void onPageSelected(int position) {



        // 设置底部小点选中状态

        setCurrentDot(position);

    }



    // 当滑动状态改变时调用

    @Override

    public void onPageScrollStateChanged(int state) {



    }

}